package com.woniuxy.bases.controller;

import com.woniuxy.bases.entity.Area;
import com.woniuxy.bases.entity.Lines;
import com.woniuxy.bases.entity.Material;
import com.woniuxy.bases.entity.Type;
import com.woniuxy.bases.entity.dto.*;
import com.woniuxy.bases.entity.vo.AreaDelVo;
import com.woniuxy.bases.service.AreaService;
import com.woniuxy.bases.service.LinesService;
import com.woniuxy.bases.service.MaterialService;
import com.woniuxy.bases.service.TypeService;
import com.woniuxy.comments.util.CustomPage;
import com.woniuxy.comments.util.ResponseResult;
import com.woniuxy.comments.util.ResponseState;
import org.apache.ibatis.annotations.Delete;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/bses")
@RestController
public class BasesController {
    @Resource
    private AreaService areaService;
    @Resource
    private LinesService linesService;
    @Resource
    private MaterialService materialService;
    @Resource
    private TypeService typeService;

    /**
     * 查询全部地区
     */
    @GetMapping("/getAllArea")
    public ResponseResult<CustomPage<List<Area>>> getAllArea(@RequestBody GetAreaDTO getAreaDTO) {
        if (getAreaDTO.getPage().getPageIndex() == null) {
            getAreaDTO.getPage().setPageIndex(1);
        }
        if (getAreaDTO.getPage().getPageSize() == null) {
            getAreaDTO.getPage().setPageSize(10);
        }
        return areaService.getAllArea(getAreaDTO);
    }

    /**
     * 批量禁用地区
     * 禁用之后也需要修改相关数据的状态为不可用
     */
    @PostMapping("/banAreas")
    public ResponseResult<CustomPage<List<Area>>> banAreas(@RequestBody List<AreaDelVo> list) {

        boolean flag = areaService.banAreas(list);
        if (flag) {
            return areaService.getAllArea(new GetAreaDTO());
        } else {
            return new ResponseResult<CustomPage<List<Area>>>()
                    .setData(new CustomPage<List<Area>>())
                    .setMessage("fail")
                    .setCode(500)
                    .setState(ResponseState.UPDATE_FAIL);
        }
    }

    /**
     * 新增地区
     */
    @PostMapping("/addNewAreas")
    public ResponseResult<CustomPage<List<Area>>> addNewAreas(@RequestBody List<Area> list) {
        boolean flag = areaService.addNewAreas(list);
        if (flag) {
            return areaService.getAllArea(new GetAreaDTO());
        } else {
            return new ResponseResult<CustomPage<List<Area>>>()
                    .setData(null)
                    .setMessage("fail")
                    .setCode(500)
                    .setState(ResponseState.UPDATE_FAIL);
        }
    }

    /**
     * 批量删除地区
     * 删除地区之后。其余使用过这个地区的数据均需要修改状态，重新选择地区
     */
    @PostMapping("/delAreas")
    public ResponseResult<CustomPage<List<Area>>> delAreas(@RequestBody List<AreaDelVo> list) {
        boolean flag = areaService.delAreas(list);
        if (flag) {
            return areaService.getAllArea(new GetAreaDTO());
        } else {
            return new ResponseResult<CustomPage<List<Area>>>()
                    .setData(null)
                    .setMessage("fail")
                    .setCode(500)
                    .setState(ResponseState.UPDATE_FAIL);
        }
    }

    /**
     * 修改描述
     */
    @PostMapping("/updateArea")
    public ResponseResult<CustomPage<List<Area>>> updateArea(@RequestBody AreaUpdateDTO areaUpdateDTO) {
        if (areaUpdateDTO != null) {
            if (areaService.updateArea(areaUpdateDTO)) {
                return areaService.getAllArea(new GetAreaDTO());
            } else {
                return new ResponseResult<CustomPage<List<Area>>>().setMessage("修改失败");
            }
        } else {
            return new ResponseResult<CustomPage<List<Area>>>().setMessage("数据呢？");
        }
    }
    /***********分割线--生产线相关*********************************************************************************************/
    /**
     * 获取全部的生产线，默认查第一页，查询十条
     *
     * @param lineGetDTO
     * @return
     */
    @GetMapping("/getAllLines")
    public ResponseResult<CustomPage<List<Lines>>> getAllLines(@RequestBody LineGetDTO lineGetDTO) {
        if (lineGetDTO.getPage().getPageIndex() == null) {
            lineGetDTO.getPage().setPageIndex(1);
        }
        if (lineGetDTO.getPage().getPageSize() == null) {
            lineGetDTO.getPage().setPageSize(10);
        }
        return linesService.getAllLines(lineGetDTO);
    }

    /**
     * 批量禁用生产线
     * 禁用生产线之后需要将对应数据状态改为不可用
     */
    @PostMapping("/banLines")
    public ResponseResult<CustomPage<List<Lines>>> banLines(@RequestBody List list) {
        boolean flag = linesService.banLines(list);
        if (flag) {
            return linesService.getAllLines(new LineGetDTO());
        } else {
            return new ResponseResult<CustomPage<List<Lines>>>()
                    .setData(null)
                    .setCode(500);
        }
    }

    /**
     * 添加生产线
     */
    @PostMapping("/addNewLines")
    public ResponseResult<CustomPage<List<Lines>>> addNewLines(@RequestBody LinesAddDTO linesAddDTO) {
        boolean flag = linesService.addNewLines(linesAddDTO);
        if (flag) {
            return linesService.getAllLines(new LineGetDTO());
        } else {
            return new ResponseResult<>();
        }
    }

    /**
     * 批量删除生产线
     * 后续需要删除和这条生产线相关的数据，所有经过这条生产线的产品都需要修改状态
     *
     * @param list
     * @return
     */
    @PostMapping("/delLines")
    public ResponseResult<CustomPage<List<Lines>>> delLines(@RequestBody List list) {
        boolean flag = linesService.delLines(list);
        if (flag) {
            return linesService.getAllLines(new LineGetDTO());
        } else {
            return new ResponseResult<>();
        }
    }

    /**
     * 修改单个生产线的描述
     *
     * @param linesUpdateDTO
     * @return
     */
    @PostMapping("/updateLines")
    public ResponseResult<CustomPage<List<Lines>>> updateLines(@RequestBody LinesUpdateDTO linesUpdateDTO) {
        if (linesUpdateDTO != null) {
            if (linesService.updateLines(linesUpdateDTO)) {
                return linesService.getAllLines(new LineGetDTO());
            } else {
                return new ResponseResult<CustomPage<List<Lines>>>().setMessage("fail");
            }
        } else {
            return new ResponseResult<CustomPage<List<Lines>>>().setMessage("massage wrong");
        }
    }
    /**--------------------------分割线--物料分类相关----------------------------------------------------**/
    /**
     * 获取全部的物料
     */
    @GetMapping("/getAllMaterial")
    public ResponseResult<CustomPage<List<Material>>> getAllMaterial(@RequestBody MaterialGetDTO materialGetDTO) {
        if (materialGetDTO.getPage().getPageIndex() == null) {
            materialGetDTO.getPage().setPageIndex(1);
        }
        if (materialGetDTO.getPage().getPageSize() == null) {
            materialGetDTO.getPage().setPageSize(10);
        }
        return materialService.getAllMaterial(materialGetDTO);
    }

    /**
     * 新增一个物料分类
     */
    @PostMapping("/addNewMaterial")
    public ResponseResult<CustomPage<List<Material>>> addNewMaterial(@RequestBody @Validated MaterialAddDTO materialAddDTO) {
        if (materialService.addNewMaterial(materialAddDTO)) {
            return materialService.getAllMaterial(new MaterialGetDTO());
        } else {
            return new ResponseResult<CustomPage<List<Material>>>().setMessage("信息有误");
        }
    }

    /**
     * 删除一个物料分类
     * 后续联调，删除该材料之后其他使用过这个材料的数据均修改状态
     */
    @DeleteMapping("/delMaterial")
    public ResponseResult<CustomPage<List<Material>>> delMaterial(Integer materialId) {
        boolean flag = materialService.delMaterial(materialId);
        ResponseResult<CustomPage<List<Material>>> result = new ResponseResult<>();
        if (flag) {
            result = materialService.getAllMaterial(new MaterialGetDTO());
        }
        return result;
    }

    @PostMapping("/updateMaterial")
    public ResponseResult<CustomPage<List<Material>>> updateMaterial(@RequestBody MaterialUpdateDTO materialUpdateDTO) {
        boolean flag = materialService.updateMaterial(materialUpdateDTO);
        ResponseResult<CustomPage<List<Material>>> result = new ResponseResult<>();
        if (flag) {
            result = materialService.getAllMaterial(new MaterialGetDTO());
        }
        return result;
    }

/**--------------------------------分割线--品类模块-----------------------------------------------------------*/
    /**
     * 新增
     */
    @PostMapping("/addType")
    public ResponseResult<CustomPage<List<Type>>> addType(@RequestBody TypeAddDTO typeAddDTO) {
        boolean flag = typeService.addType(typeAddDTO);
        ResponseResult<CustomPage<List<Type>>> result = new ResponseResult<>();
        if (flag) {
            result = typeService.getAllType(new TypeGetDTO());
        }
        return result;
    }

    /**
     * 查询
     */
    @PostMapping("/getAllType")
    public ResponseResult<CustomPage<List<Type>>> getAllType(@RequestBody TypeGetDTO typeGetDTO) {
        if (typeGetDTO.getPage().getPageSize() == null) {
            typeGetDTO.getPage().setPageSize(10);
        }
        if (typeGetDTO.getPage().getPageIndex() == null) {
            typeGetDTO.getPage().setPageIndex(1);
        }
        return typeService.getAllType(typeGetDTO);
    }

    /**
     * 删除
     * 后续需要修改与之相关的阿类型的数据的状态
     */
    @DeleteMapping("/delType")
    public ResponseResult<CustomPage<List<Type>>> delType(Integer typeId){
    ResponseResult<CustomPage<List<Type>>> result=new ResponseResult<>();
        if(typeService.delType(typeId)){
            result=typeService.getAllType(new TypeGetDTO());
        }
        return result;
    }
    /**
     * 改
     */
    @PostMapping("/updateType")
    public ResponseResult<CustomPage<List<Type>>> updateType(@RequestBody TypeUpdateDTO typeUpdateDTO){
        ResponseResult<CustomPage<List<Type>>> result=new ResponseResult<>();
        if(typeService.updateType(typeUpdateDTO)){
            result=typeService.getAllType(new TypeGetDTO());
        }
        return result;
    }
}
